package com.csdj.t301.service;

import com.mysql.cj.log.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;

import javax.annotation.PostConstruct;
import javax.xml.transform.Result;
import java.util.concurrent.TimeUnit;

/**
 * author:cloud
 * date:2021/4/9 0009
 **/
@Service("cache")
public class RedisService implements ICache{

    @Value("${redis.host}")
    private String host;


    private JedisPool pool;
    public static final String RESULT_OK = "OK";

    @PostConstruct
    public void init(){
        JedisPoolConfig config = new JedisPoolConfig();
//        连接池最大连接数
        config.setMaxTotal(20);
//        连接池中最大空闲连接数
        config.setMaxIdle(2);
//        连接池中最小空闲连接数
        config.setMinIdle(1);
//        从连接池获取连接时进行测试
        config.setTestOnBorrow(true);
//        每次对连接池扫描的连接的数量 -1：没有限制
        config.setNumTestsPerEvictionRun(-1);
//        两次扫描的时间间隔 以毫秒为单
        config.setTimeBetweenEvictionRunsMillis(60000);
//        当连接空闲时多长时间才进行扫描
        config.setMinEvictableIdleTimeMillis(30000);
//        当所有连接都在使用，再向连接池获取连接则进行等待
        config.setBlockWhenExhausted(false);
        pool = new JedisPool(config,"127.0.0.1",6379,30,"123456",0);
    }


    public static void main(String[] args) {
        RedisService cache = new RedisService();
        cache.init();
        String res = cache.get("name");
        System.out.println(res);

    }

    @Override
    public String get(String key) {
        try(Jedis resource = pool.getResource()){
            return resource.get(key);
        }
    }

    @Override
    public boolean set(String key, String value) {
        try(Jedis client = pool.getResource()){
            return RESULT_OK.equals(client.set(key,value));
        }
        //jdk1.7之前的写法，
//        Jedis client = null;
//        try{
//            client = pool.getResource();
//            return RESULT_OK.equals(client.set(key,value));
//        }finally {
//            if(client != null){
//                client.close();
//            }
//        }
    }

    @Override
    public boolean set(String key, String value, int seconds) {
        try(final Jedis jedis = pool.getResource()){
            return RESULT_OK.equals(jedis.setex(key,seconds,value));
        }
    }



    //chanel 通道  topic 主题
    @Override
    public void sub(){
        try(Jedis jedis = pool.getResource()){
            new Thread(()->{
                jedis.subscribe(new JedisPubSub() {
                    @Override
                    public void onMessage(String channel, String message) {
                        System.out.println("收到消息："+message);
                        try {
                            TimeUnit.SECONDS.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                },"channel");
            }).start();
        }
    }
}
